// src/api/HotelApi.ts
import { BaseApiClient } from '../BaseApi.ts';
import type { HotelData, ApiResponse } from '../../types/DataTypes.ts';

// 酒店相关API
export class HotelApi extends BaseApiClient {
    constructor() {
        super();
    }

    // 获取所有酒店
    async getAllHotels(): Promise<ApiResponse<HotelData[]>> {
        return this.request<HotelData[]>({
            method: 'GET',
            url: '/hotels',
        });
    }

    // 高级搜索酒店（支持位置、价格范围）
    async advancedSearch(criteria: {
        location?: string;
        minPrice?: number;
        maxPrice?: number;
    }): Promise<ApiResponse<HotelData[]>> {
        const params = new URLSearchParams();
        
        if (criteria.location) params.append('location', criteria.location);
        if (criteria.minPrice) params.append('minPrice', criteria.minPrice.toString());
        if (criteria.maxPrice) params.append('maxPrice', criteria.maxPrice.toString());

        return this.request<HotelData[]>({
            method: 'GET',
            url: `/hotels/search/advanced?${params.toString()}`,
        });
    }

    // 添加新酒店
    async addHotel(hotel: Omit<HotelData, 'availRoom'> & { availRoom?: number }): Promise<ApiResponse<HotelData>> {
        return this.request<HotelData>({
            method: 'POST',
            url: '/hotels',
            data: {
                ...hotel,
                availRoom: hotel.availRoom ?? hotel.totalRoom, // 默认可用房间等于总数
            },
        });
    }

    // 更新酒店信息
    async updateHotel(hotelData: HotelData): Promise<ApiResponse> {
        return this.request({
            method: 'PUT',
            url: '/hotels/update',
            data: hotelData,
        });
    }

    // 删除酒店
    async deleteHotel(location: string): Promise<ApiResponse> {
        return this.request({
            method: 'DELETE',
            url: `/hotels/${encodeURIComponent(location)}`,
        });
    }
}

// 创建单例实例
export const hotelApi = new HotelApi();

// 默认导出
export default hotelApi;